package com.tao.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tao.common.constant.WareConstant;
import com.tao.gulimall.ware.entity.PurchaseDetailEntity;
import com.tao.gulimall.ware.service.PurchaseDetailService;
import com.tao.gulimall.ware.service.WareSkuService;
import com.tao.gulimall.ware.vo.MergeVo;
import com.tao.gulimall.ware.vo.PurchaseDoneVo;
import com.tao.gulimall.ware.vo.PurchaseItemDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.common.utils.PageUtils;
import com.tao.common.utils.Query;

import com.tao.gulimall.ware.dao.PurchaseDao;
import com.tao.gulimall.ware.entity.PurchaseEntity;
import com.tao.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

	@Autowired
	private PurchaseDetailService purchaseDetailService;
	@Autowired
	private WareSkuService wareSkuService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public PageUtils queryPageUnreceive(Map<String, Object> params) {
		LambdaQueryWrapper<PurchaseEntity> lqw = new LambdaQueryWrapper<>();
		lqw.eq(PurchaseEntity::getStatus,0).or().eq(PurchaseEntity::getStatus,1);
		IPage<PurchaseEntity> page = this.page(
				new Query<PurchaseEntity>().getPage(params),
				lqw
		);

		return new PageUtils(page);
	}

	@Transactional
	@Override
	public void merge(MergeVo mergeVo) {
		Long finalPurchaseId = null;
		List<Long> items = mergeVo.getItems();
		if (mergeVo.getPurchaseId() == null){
			// 需要自己新建一个采购单然后合并
			PurchaseEntity purchaseEntity = new PurchaseEntity();
			purchaseEntity.setCreateTime(new Date());
			purchaseEntity.setUpdateTime(new Date());
			purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
			this.save(purchaseEntity);
			finalPurchaseId = purchaseEntity.getId();
		}
		finalPurchaseId = mergeVo.getPurchaseId();
		// 使用现有的采购单进行合并
		Long finalPurchaseId1 = finalPurchaseId;
		List<PurchaseDetailEntity> collect = items.stream().map(item -> {
			PurchaseDetailEntity purchaseDetail = new PurchaseDetailEntity();
			purchaseDetail.setPurchaseId(finalPurchaseId1);

			purchaseDetail.setId(item);
			purchaseDetail.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
			return purchaseDetail;
		}).collect(Collectors.toList());

		purchaseDetailService.updateBatchById(collect);

		PurchaseEntity purchaseEntity = new PurchaseEntity();
		purchaseEntity.setUpdateTime(new Date());
		purchaseEntity.setId(finalPurchaseId1);
		this.updateById(purchaseEntity);
	}

	@Override
	public void received(List<Long> ids) {
		// 确认当前采购单是新建或者已分配状态
		// 筛选出需要修改状态的采购单
		List<PurchaseEntity> purchaseEntities = ids.stream().map(id -> {
			return this.getById(id);
		}).filter(item -> {
			if (item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() ||
					item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()) {
				return true;
			}
			return false;
		}).map(item -> {
			item.setCreateTime(new Date());
			item.setStatus(WareConstant.PurchaseStatusEnum.FINISH.getCode());
			return item;
		}).collect(Collectors.toList());


		// 改变采购单的状态
		this.updateBatchById(purchaseEntities);

		// 改变采购单绑定的采购项的状态
		purchaseEntities.stream().forEach(item->{
			List<PurchaseDetailEntity> list = purchaseDetailService.seletListByPurchaseId(item.getId());
			List<PurchaseDetailEntity> collect = list.stream().map(item2 -> {
				PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
				purchaseDetailEntity.setId(item2.getId());
				purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
				return purchaseDetailEntity;
			}).collect(Collectors.toList());
			purchaseDetailService.updateBatchById(collect);
		});
	}

	@Override
	@Transactional
	public void done(PurchaseDoneVo purchaseDoneVo) {
		Long id = purchaseDoneVo.getId();
		List<PurchaseItemDoneVo> items = purchaseDoneVo.getItems();
		// 改变采购项的状态
		List<PurchaseDetailEntity> list = new ArrayList<>();
		Boolean flag = true;
		for (PurchaseItemDoneVo item : items) {
			PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
			if(item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
				// 出现问题, 也就是当前采购项没成功
				flag = false;
				// 同时还是要保留之前的状态
				purchaseDetailEntity.setStatus(item.getStatus());
			}else {
				// 当前采购项成功了
				// 更新采购项状态
				purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
				// 采购成功同时需要对库存进行操作
				//  得到当前采购项的详细信息
				PurchaseDetailEntity byId = purchaseDetailService.getById(item.getItemId());
				wareSkuService.addStock(byId.getSkuId(),byId.getWareId(),byId.getSkuNum());

			}
			purchaseDetailEntity.setId(item.getItemId());
			list.add(purchaseDetailEntity);
		}
		purchaseDetailService.updateBatchById(list);



		// 改变采购单的状态, 因为是基于采购项的状态决定采购单的状态的
		PurchaseEntity purchaseEntity = new PurchaseEntity();
		purchaseEntity.setId(id);
		purchaseEntity.setUpdateTime(new Date());
		purchaseEntity.setStatus(flag?WareConstant.PurchaseStatusEnum.FINISH.getCode() : WareConstant.PurchaseStatusEnum.HASERROR.getCode());
		this.updateById(purchaseEntity);

		// 将成功采购的进行入库
	}
}